import sys
import os
import struct
import time

file_version = 0x01000411

#print('参数个数为：', len(sys.argv))
#print('参数列表  ：', str(sys.argv))
'''
if (len(sys.argv) == 5): #windows手动输入命令行参数
    if (sys.argv[1]!="-i"):
        print('Usage err! input file err!')
        print('please try [xxx.py -i infile.bin -o outfile.bin]')
        os._exit(1)
    elif (sys.argv[3]!="-o"):
        print('Usage err! output file err!')
        print('please try [xxx.py -i infile.bin -o outfile.bin]')
        os._exit(1)
    source_file = "./"+sys.argv[2]  #源文件路径
    des_file = "./"+sys.argv[4]     #目标文件路径
'''
if (len(sys.argv) == 1): #windows下双击该文件运行
    source_file = input("please input source file name   :")
    if (os.path.isfile(source_file) == False): #文件不存在
        print(source_file+' is not exists, please check your input!')
        os.system('pause')
        os._exit(1)
    #获取版本
    tmp_str     = "."
    tmp_version = input("please input source file version:")
    index       = tmp_version.index(tmp_str)
    version1    = tmp_version[:index]

    tmp_version = tmp_version[index+1:]
    index       = tmp_version.index(tmp_str)
    version2    = tmp_version[:index]

    tmp_version = tmp_version[index+1:]
    index       = tmp_version.index(tmp_str)
    version3    = tmp_version[:index]

    #tmp_version = tmp_version[index+1:]
    #index       = tmp_version.index(tmp_str)
    #version4    = tmp_version[:index]
    version4    = tmp_version[index+1:]

    file_version = (int(version1) << 24) + (int(version2) << 16) + (int(version3) << 8) + int(version4)
    #print(version1)
    #print(version2)
    #print(version3)
    #print(version4)
    #print(file_version)
    #os.system('pause')
    #构建目标文件名
    des_file    = "packed"+source_file
else:
    print('Usage err! input para num err!')
    print('please try [xxx.py]')
    '''
    print('or')
    print('please try [xxx.py -i infile.bin -o outfile.bin]')
    '''
    os._exit(1)


data = 0x00
sum  = 0x5555

f_source = open(source_file,'rb')
f_des = open(des_file,'wb+')

source_size = os.path.getsize(source_file)

#填充0x100长度的文件头，默认全0
for i in range(0x100):
  f_des.write(struct.pack('b', 0x00))

#从源文件拷贝文件到目标文件
for i in range(source_size):
  data = f_source.read(1)
  sum  += int.from_bytes(data, byteorder='big', signed=False)
  f_des.write(data)

#在文件头中写入文件长度
f_des.seek(0, 0)
f_des.write(struct.pack('<I', source_size&0xFFFFFFFF))

#在文件头中写入文件版本
f_des.seek(4, 0)
f_des.write(struct.pack('>I', file_version&0xFFFFFFFF))

#在文件头中写入文件校验和
f_des.seek(130, 0)
f_des.write(struct.pack('<H', sum&0xFFFF))

#在文件头中写入文件版本
f_des.seek(132, 0)
f_des.write(struct.pack('>I', file_version&0xFFFFFFFF))

#计算文件头的校验和
sum  = 0x5555
f_des.seek(0, 0)
for i in range(0x100):
  data = f_des.read(1)
  sum  += int.from_bytes(data, byteorder='big', signed=False)

#写入文件头的校验和
f_des.seek(254, 0)
f_des.write(struct.pack('<H', sum&0xFFFF))

f_source.close()
f_des.close()

print ("源文件  ：", f_source.name)
print ("目标文件：", f_des.name)
print ("文件版本：", (file_version>>24)&0xFF, (file_version>>16)&0xFF, (file_version>>8)&0xFF, file_version&0xFF)

os.system('pause')

